package com.xueyi.aixihu.mapper;

import com.xueyi.aixihu.entity.vo.DtFeedBackTotalVo;
import com.xueyi.aixihu.entity.vo.DtFeedBackVo;
import com.xueyi.aixihu.entity.vo.DtyybmVo;
import com.xueyi.aixihu.reqeust.DtFindByFeedBackListReq;
import com.xueyi.aixihu.reqeust.InsertMessageContentReq;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * @author yuyu
 * @date : 2022/8/16
 * @Description : 留言反馈的mapper层
 */
public interface LocalDtFeedBackMapper {
    /**
     * 查询留言反馈的集合
     *
     * @param dtFindByFeedBackListReq
     * @return
     */
    @Select("<script>" +
            " select dy.* from dt_yybm dy  " +
            " <where> " +
            " <if test = 'dtFindByFeedBackListReq.name != null' > " +
            " dy.member_name like concat ('%',#{dtFindByFeedBackListReq.name},'%' ) " +
            " </if> " +
            " <if test = 'dtFindByFeedBackListReq.status != null' > " +
            " and dy.status in (${dtFindByFeedBackListReq.status}) " +
            " </if> " +
            " <if test = 'userAddressId != null' > " +
            " and dy.from_to_area_id in (${userAddressId}) " +
            " </if> " +
            " </where> " +
            " order by dy.create_time desc " +
            " limit #{dtFindByFeedBackListReq.startIndex}, #{dtFindByFeedBackListReq.limit} " +
            "</script>")
    public List<DtFeedBackVo> findByFeedBackList(@Param("dtFindByFeedBackListReq") DtFindByFeedBackListReq dtFindByFeedBackListReq, @Param("userAddressId") String userAddressId);

    /**
     * 查询留言反馈的条数
     *
     * @param dtFindByFeedBackListReq
     * @return
     */
    @Select("<script>" +
            " select count(dy.id) from dt_yybm dy  " +
            " <where> " +
            " <if test = 'dtFindByFeedBackListReq.name != null' > " +
            " dy.member_name like concat ('%',#{dtFindByFeedBackListReq.name},'%' ) " +
            " </if> " +
            " <if test = 'dtFindByFeedBackListReq.status != null' > " +
            " and dy.status in (${dtFindByFeedBackListReq.status}) " +
            " </if> " +
            " <if test = 'userAddressId != null' > " +
            " and dy.from_to_area_id in (${userAddressId}) " +
            " </if> " +
            " </where> " +
            "</script>")
    public Integer findByFeedBackListCount(@Param("dtFindByFeedBackListReq") DtFindByFeedBackListReq dtFindByFeedBackListReq, @Param("userAddressId") String userAddressId);

    /**
     * 查看单个留言具体信息
     *
     * @param id
     * @return
     */
    @Select("<script>" +
            " select * from dt_yybm where id = #{id} " +
            "</script>")
    public DtyybmVo viewInformation(Integer id);

    /**
     * 改变留言状态
     *
     * @param id
     * @param isValid
     */
    @Update(" update dt_yybm set status = #{isValid} where id = #{id} ")
    public void updateIsValid(Integer id, Integer isValid);

    /**
     * 查询留言反馈的各种状态的条数
     *
     * @return
     */
    @Select("<script>" +
            " select count(id) allTotal, " +
            " SUM(IF(status = 1,1,NULL)) alreadyFeedBackTotal, " +
            " SUM(IF(status = 0,1,NULL)) waitFeedBackTotal  " +
            " FROM dt_yybm  " +
            " <if test = 'name != null'> " +
            " where member_name LIKE '%${name}%' " +
            " </if> " +
            "</script>")
    public DtFeedBackTotalVo findFeedBackTotal(@Param("name") String name, String userAddressId);

    /**
     * 回复留言信息
     *
     * @param id
     * @param content
     */
    @Update(" update dt_yybm set status = 1 , shenhe_time=NOW() ,admin_remark = #{content} where id = #{id} ")
    public void updateLeaving(Integer id, String content);

    /**
     * 发送留言信息
     *
     * @param insertMessageContentReq
     */
    @Insert(" INSERT INTO dt_yybm (member_name,task_name,status,create_time) " +
            " VALUES (#{nickName},#{messageContent},0,NOW()) ")
    void insertMessageContent(InsertMessageContentReq insertMessageContentReq);

}
